package com.mdd.front.service.impl.yangyangLeadingEnterprise;

import com.mdd.common.entity.yangyangLeadingEnterprise.TAppEnterprise;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppEnterpriseUser;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppUser;
import com.mdd.common.entity.yangyangLeadingEnterprise.common.JsonReturn;
import com.mdd.common.entity.yangyangLeadingEnterprise.param.EnterpriseUserParam;
import com.mdd.common.mapper.yangyangLeadingEnterprise.TAppEnterpriseMapper;
import com.mdd.common.mapper.yangyangLeadingEnterprise.TAppEnterpriseUserMapper;
import com.mdd.common.mapper.yangyangLeadingEnterprise.TAppUserMapper;
import com.mdd.common.util.RedisUtils;
import com.mdd.common.util.qichacha.ECIThreeElVerifyUtil;
import com.mdd.front.service.yangyangLeadingEnterprise.ITAppEnterpriseUserService;
import org.apache.commons.math3.util.Pair;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 【请填写功能名称】实现类
 * @author LikeAdmin
 */
@Service
public class TAppEnterpriseUserServiceImpl implements ITAppEnterpriseUserService {
        
    @Resource
    TAppEnterpriseUserMapper tAppEnterpriseUserMapper;

    @Resource
    TAppEnterpriseMapper appEnterpriseMapper;

    @Resource
    TAppUserMapper  appUserMapper;

    @Resource
    TAppUserServiceImpl appUserService;

    /**
     * 用户（法人）绑定企业信息
     * @param param
     * @return
     */
    @Override
    public String insertEnterpriseUser(EnterpriseUserParam param) {
//        Integer userId = param.getUserId();
        String token = param.getToken();
        String enterpriseName = param.getEnterpriseName();
        String enterpriseTaxNumber = param.getEnterpriseTaxNumber();
        Integer dicType = param.getDicType();
        String phone = param.getPhone();
        String code = param.getCode();
        String authenticationMethod = param.getAuthenticationMethod();
        if (token == null || token.isEmpty() || enterpriseName == null || enterpriseName.isEmpty() || code==null || code.isEmpty()
                || phone == null || phone.isEmpty() || dicType == null || authenticationMethod == null || authenticationMethod.isEmpty() || enterpriseTaxNumber==null || enterpriseTaxNumber.isEmpty()) {
            return JsonReturn.fail("必传参数不能为空！",  40001).toJsonStr();
        }
//        if(!RedisUtils.exists(phone)){
//            return JsonReturn.fail("验证码已过期",40002).toJsonStr();
//        }
//        Object codeKeyData =  RedisUtils.get(phone);
//        if(codeKeyData==null){
//            return JsonReturn.fail("验证码已失效",40003).toJsonStr();
//        }
//        if(!code.equals(codeKeyData)){
//            return JsonReturn.fail("验证码错误",40004).toJsonStr();
//        }
        RedisUtils.del(phone);
        TAppUser appUser = appUserService.selectUserByToken(token);
        Integer realNameFlag = appUser.getRealNameFlag();
        if (realNameFlag==null || realNameFlag != 1) {
            return JsonReturn.fail("请先完成实名认证！",  40005).toJsonStr();
        }
        Integer faceFlag = appUser.getFaceFlag();
        if (faceFlag==null || faceFlag != 1) {
            return JsonReturn.fail("请先完成人脸活体认证！",  40006).toJsonStr();
        }
        Long userId = appUser.getId();
        String name = appUser.getRealName();
        String idNumber = appUser.getCertNo();
        //todo 企业三要素校验
//        Pair<Boolean, String> pair = ECIThreeElVerifyUtil.GetInfo(enterpriseName, enterpriseTaxNumber, name);
//        if(!pair.getKey()){
//            return JsonReturn.fail(pair.getValue(),  40007).toJsonStr();
//        }
        Date now = new Date();
        TAppEnterprise enterprise = new TAppEnterprise();
        enterprise.setUserId(userId);
        enterprise.setEnterpriseName(enterpriseName);
        enterprise.setEnterpriseLegalPerson(name);
        enterprise.setIdNumber(idNumber);
        enterprise.setCreateTime(now);
        enterprise.setUpdateTime(now);
        appEnterpriseMapper.insert(enterprise);

        TAppEnterpriseUser enterpriseUser = new TAppEnterpriseUser();
        enterpriseUser.setName(name);
        enterpriseUser.setIdNumber(idNumber);
        enterpriseUser.setUserId(userId);
        enterpriseUser.setEnterpriseId(enterprise.getId());
        enterpriseUser.setEnterpriseName(enterpriseName);
        enterpriseUser.setPhone(phone);
        enterpriseUser.setDicType(dicType);
        enterpriseUser.setAuthenticationMethod(authenticationMethod);
        enterpriseUser.setCreateTime(now);
        enterpriseUser.setUpdateTime(now);
        enterpriseUser.setFaceFlag(1);
        enterpriseUser.setRealNameFlag(1);
        enterpriseUser.setEnterpriseCertificationFlag(1);
        tAppEnterpriseUserMapper.insert(enterpriseUser);
        return JsonReturn.success().toJsonStr();
    }

}
